package com.guchenbo.example.dubbo.cluster;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.guchenbo.example.dubbo.template.DirectoryTemplate;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.cluster.Router;

import java.util.List;

public class GcbDirectory<T> extends DirectoryTemplate<T> {

  private List<Invoker<T>> invokers;
  private Router router;

  public GcbDirectory(URL url, List<Invoker<T>> invokers) {
    super(url);
    this.invokers = invokers;
    this.router = new GcbRouter(url);
  }

  @Override
  protected List<Invoker<T>> doList(Invocation invocation) {
    Preconditions.checkNotNull(invokers);
    List<Invoker<T>> list = Lists.newArrayList(invokers);
    // filter by route
    list = router.route(list, getUrl(), invocation);
    return list;
  }

  @Override
  public List<Invoker<T>> getAllInvokers() {
    return invokers;
  }

  @Override
  public Class<T> getInterface() {
    return invokers.get(0).getInterface();
  }

  @Override
  public URL getConsumerUrl() {
    return null;
  }
}
